home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Modules / syslogmodule.c < prev    next >
C/C++ Source or Header  |  1999-04-27  |  7KB  |  273 lines

  1. /***********************************************************
  2. Copyright 1994 by Lance Ellinghouse,
  3. Cathedral City, California Republic, United States of America.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the name of Lance Ellinghouse
  12. not be used in advertising or publicity pertaining to distribution 
  13. of the software without specific, written prior permission.
  14.  
  15. LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL, 
  18. INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 
  19. FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
  20. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
  21. WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /******************************************************************
  26.  
  27. Revision history:
  28.  
  29. 1998/04/28 (Sean Reifschneider)
  30.   - When facility not specified to syslog() method, use default from openlog()
  31.     (This is how it was claimed to work in the documentation)
  32.   - Potential resource leak of o_ident, now cleaned up in closelog()
  33.   - Minor comment accuracy fix.
  34.  
  35. 95/06/29 (Steve Clift)
  36.   - Changed arg parsing to use PyArg_ParseTuple.
  37.   - Added PyErr_Clear() call(s) where needed.
  38.   - Fix core dumps if user message contains format specifiers.
  39.   - Change openlog arg defaults to match normal syslog behaviour.
  40.   - Plug memory leak in openlog().
  41.   - Fix setlogmask() to return previous mask value.
  42.  
  43. ******************************************************************/
  44.  
  45. /* syslog module */
  46.  
  47. #include "Python.h"
  48.  
  49. #include <syslog.h>
  50. #if defined(AMITCP) || defined(INET225)
  51. #include <proto/socket.h>
  52. #endif
  53.  
  54. #include "protos/syslogmodule.h"
  55.  
  56. /*  only one instance, only one syslog, so globals should be ok  */
  57. static PyObject *S_ident_o = NULL;            /*  identifier, held by openlog()  */
  58.  
  59.  
  60. #ifndef INET225
  61. static PyObject * 
  62. syslog_openlog(self, args)
  63.     PyObject * self;
  64.     PyObject * args;
  65. {
  66.     long logopt = 0;
  67.     long facility = LOG_USER;
  68.  
  69.  
  70.     Py_XDECREF(S_ident_o);
  71.     if (!PyArg_ParseTuple(args,
  72.                   "S|ll;ident string [, logoption [, facility]]",
  73.                   &S_ident_o, &logopt, &facility))
  74.         return NULL;
  75.  
  76.     /* This is needed because openlog() does NOT make a copy
  77.      * and syslog() later uses it.. cannot trash it.
  78.      */
  79.     Py_INCREF(S_ident_o);
  80.  
  81.     openlog(PyString_AsString(S_ident_o), logopt, facility);
  82.  
  83.     Py_INCREF(Py_None);
  84.     return Py_None;
  85. }
  86. #endif /* !INET225 */
  87.  
  88. static PyObject * 
  89. syslog_syslog(self, args)
  90.     PyObject * self;
  91.     PyObject * args;
  92. {
  93.     char *message;
  94.     int   priority = LOG_INFO;
  95.  
  96.     if (!PyArg_ParseTuple(args, "is;[priority,] message string",
  97.                   &priority, &message)) {
  98.         PyErr_Clear();
  99.         if (!PyArg_ParseTuple(args, "s;[priority,] message string",
  100.                       &message))
  101.             return NULL;
  102.     }
  103.  
  104.     syslog(priority, "%s", message);
  105.     Py_INCREF(Py_None);
  106.     return Py_None;
  107. }
  108.  
  109. #ifndef INET225
  110. static PyObject * 
  111. syslog_closelog(self, args)
  112.     PyObject * self;
  113.     PyObject * args;
  114. {
  115.     if (!PyArg_ParseTuple(args, ""))
  116.         return NULL;
  117.     closelog();
  118.     Py_XDECREF(S_ident_o);
  119.     S_ident_o = NULL;
  120.     Py_INCREF(Py_None);
  121.     return Py_None;
  122. }
  123.  
  124. static PyObject * 
  125. syslog_setlogmask(self, args)
  126.     PyObject * self;
  127.     PyObject * args;
  128. {
  129.     long maskpri, omaskpri;
  130.  
  131.     if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
  132.         return NULL;
  133.     omaskpri = setlogmask(maskpri);
  134.     return PyInt_FromLong(omaskpri);
  135. }
  136.  
  137. static PyObject * 
  138. syslog_log_mask(self, args)
  139.     PyObject * self;
  140.     PyObject * args;
  141. {
  142.     long mask;
  143.     long pri;
  144.     if (!PyArg_ParseTuple(args, "l", &pri))
  145.         return NULL;
  146.     mask = LOG_MASK(pri);
  147.     return PyInt_FromLong(mask);
  148. }
  149.  
  150. static PyObject * 
  151. syslog_log_upto(self, args)
  152.     PyObject * self;
  153.     PyObject * args;
  154. {
  155.     long mask;
  156.     long pri;
  157.     if (!PyArg_ParseTuple(args, "l", &pri))
  158.         return NULL;
  159.     mask = LOG_UPTO(pri);
  160.     return PyInt_FromLong(mask);
  161. }
  162.  
  163. /* List of functions defined in the module */
  164.  
  165. static PyMethodDef syslog_methods[] = {
  166.     {"openlog",    syslog_openlog,        METH_VARARGS},
  167.     {"closelog",    syslog_closelog,    METH_VARARGS},
  168.     {"syslog",    syslog_syslog,        METH_VARARGS},
  169.     {"setlogmask",    syslog_setlogmask,    METH_VARARGS},
  170.     {"LOG_MASK",    syslog_log_mask,    METH_VARARGS},
  171.     {"LOG_UPTO",    syslog_log_upto,    METH_VARARGS},
  172.     {NULL,        NULL,            0}
  173. };
  174. #endif /* !INET225 */
  175.  
  176. #ifdef INET225
  177. /* List of functions defined in the module */
  178. /* I-Net 225 only has syslog */
  179.  
  180. static PyMethodDef syslog_methods[] = {
  181.     {"syslog",    syslog_syslog,        METH_VARARGS},
  182.     {NULL,        NULL,            0}
  183. };
  184. #endif /* INET225 */
  185.  
  186. /* helper function for initialization function */
  187.  
  188. static void
  189. ins(d, s, x)
  190.     PyObject *d;
  191.     char *s;
  192.     long x;
  193. {
  194.     PyObject *v = PyInt_FromLong(x);
  195.     if (v) {
  196.         PyDict_SetItemString(d, s, v);
  197.         Py_DECREF(v);
  198.     }
  199. }
  200.  
  201. /* Initialization function for the module */
  202.  
  203. DL_EXPORT(void)
  204. initsyslog()
  205. {
  206.     PyObject *m, *d;
  207.  
  208.     /* Create the module and add the functions */
  209.     m = Py_InitModule("syslog", syslog_methods);
  210.  
  211.     /* Add some symbolic constants to the module */
  212.     d = PyModule_GetDict(m);
  213.  
  214.     /* Priorities */
  215.     ins(d, "LOG_EMERG",    LOG_EMERG);
  216.     ins(d, "LOG_ALERT",    LOG_ALERT);
  217.     ins(d, "LOG_CRIT",    LOG_CRIT);
  218.     ins(d, "LOG_ERR",    LOG_ERR);
  219.     ins(d, "LOG_WARNING",    LOG_WARNING);
  220.     ins(d, "LOG_NOTICE",    LOG_NOTICE);
  221.     ins(d, "LOG_INFO",    LOG_INFO);
  222.     ins(d, "LOG_DEBUG",    LOG_DEBUG);
  223.  
  224.     /* openlog() option flags */
  225.     ins(d, "LOG_PID",    LOG_PID);
  226.     ins(d, "LOG_CONS",    LOG_CONS);
  227.     ins(d, "LOG_NDELAY",    LOG_NDELAY);
  228. #ifdef LOG_NOWAIT
  229.     ins(d, "LOG_NOWAIT",    LOG_NOWAIT);
  230. #endif
  231. #ifdef LOG_PERROR
  232.     ins(d, "LOG_PERROR",    LOG_PERROR);
  233. #endif
  234.  
  235.     /* Facilities */
  236.     ins(d, "LOG_KERN",    LOG_KERN);
  237.     ins(d, "LOG_USER",    LOG_USER);
  238.     ins(d, "LOG_MAIL",    LOG_MAIL);
  239.     ins(d, "LOG_DAEMON",    LOG_DAEMON);
  240.     ins(d, "LOG_AUTH",    LOG_AUTH);
  241.     ins(d, "LOG_LPR",    LOG_LPR);
  242.     ins(d, "LOG_LOCAL0",    LOG_LOCAL0);
  243.     ins(d, "LOG_LOCAL1",    LOG_LOCAL1);
  244.     ins(d, "LOG_LOCAL2",    LOG_LOCAL2);
  245.     ins(d, "LOG_LOCAL3",    LOG_LOCAL3);
  246.     ins(d, "LOG_LOCAL4",    LOG_LOCAL4);
  247.     ins(d, "LOG_LOCAL5",    LOG_LOCAL5);
  248.     ins(d, "LOG_LOCAL6",    LOG_LOCAL6);
  249.     ins(d, "LOG_LOCAL7",    LOG_LOCAL7);
  250.  
  251. #ifndef LOG_SYSLOG
  252. #define LOG_SYSLOG        LOG_DAEMON
  253. #endif
  254. #ifndef LOG_NEWS
  255. #define LOG_NEWS        LOG_MAIL
  256. #endif
  257. #ifndef LOG_UUCP
  258. #define LOG_UUCP        LOG_MAIL
  259. #endif
  260. #ifndef LOG_CRON
  261. #define LOG_CRON        LOG_DAEMON
  262. #endif
  263.  
  264.     ins(d, "LOG_SYSLOG",    LOG_SYSLOG);
  265.     ins(d, "LOG_CRON",    LOG_CRON);
  266.     ins(d, "LOG_UUCP",    LOG_UUCP);
  267.     ins(d, "LOG_NEWS",    LOG_NEWS);
  268.  
  269.     /* Check for errors */
  270.     if (PyErr_Occurred())
  271.         Py_FatalError("can't initialize module syslog");
  272. }
  273.